package com.fortune.controller;

import java.util.List;

import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DStoreDeficitExcution;
import com.fortune.dto.excution.DStoreSurplusExcution;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusAddDto;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusEditDto;
import com.fortune.dto.in.dStoreSurplus.DStoreSurplusListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DStoreDeficitEnum;
import com.fortune.enums.DStoreSurplusEnum;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.DStoreSurplusService;
import com.fortune.vo.dStoreSurplus.DStoreSurplusInfo;
import com.fortune.vo.dStoreSurplus.DStoreSurplusInfoDetail;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.fortune.base.BaseAPIExcution;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "dStoreSurplus", description = "经销商仓库报盈", tags = {"经销商仓库报盈"})
@RestController
@RequestMapping("dStoreSurplus")
public class DStoreSurplusController {
    @Autowired
    private DStoreSurplusService dStoreSurplusService;
    Logger logger= LoggerFactory.getLogger(this.getClass());

    @ApiOperation(value = "报盈单列表", nickname = "dStoreSurplusList", notes = "", tags={ "@岳希伟" })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "length", value = "每页容量", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "search", value = "搜索关键词", paramType = "query", dataType = "string", required = false)
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData> list(HttpServletRequest request, @ApiIgnore @Validated DStoreSurplusListDto dStoreSurplusListDto) {
        dStoreSurplusListDto.setPage((dStoreSurplusListDto.getPage()-1)*10);
        HttpSession session=request.getSession();
        DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String companyId=dUserInfo.getmId();
        String dealerId=dUserInfo.getdId();
        List<DStoreSurplusInfo> list = dStoreSurplusService.findSurplusList(companyId,dealerId,dStoreSurplusListDto);
        int count = dStoreSurplusService.findSurplusListCount(companyId,dealerId,dStoreSurplusListDto);
        PagingData pagingData = new PagingData();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new DStoreSurplusExcution<PagingData>(pagingData, DStoreSurplusEnum.SUCCESS);
    }

    @ApiOperation(value = "报盈单详情", nickname = "dStoreSurplusDetail", notes = "", tags={ "@岳希伟" })
    @ApiImplicitParam(name="id", value="报盈单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<DStoreSurplusInfoDetail> detail(HttpServletRequest request, @Validated @NotBlank(message="报盈单id不能为空")String id) {
        HttpSession session=request.getSession();
        DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String companyId=dUserInfo.getmId();
        String dealerId=dUserInfo.getdId();
        DStoreSurplusInfoDetail detail=dStoreSurplusService.findDetail(companyId,dealerId,id);
        return new DStoreSurplusExcution<DStoreSurplusInfoDetail>(detail, DStoreSurplusEnum.SUCCESS);
    }

   @ApiOperation(value = "添加报盈单", nickname = "dStoreSurplusAdd", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "add",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> add(HttpServletRequest request ,@Validated @RequestBody DStoreSurplusAddDto addDto) {
        HttpSession session=request.getSession();
        DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String companyId=dUserInfo.getmId();
        String dealerId=dUserInfo.getdId();
        String uid=dUserInfo.getId();
        try{
            int addSurplusBase=dStoreSurplusService.addSurplusBase(companyId,dealerId,uid,addDto);
            if(addSurplusBase>0){
                return new DStoreSurplusExcution<Void>(null, DStoreSurplusEnum.SUCCESS);
            }else {
                return new DStoreSurplusExcution<Void>(null, DStoreSurplusEnum.FAILURE);
            }
        }catch (InsertInnerErrorException e){
            logger.error(e.getMessage(), e);
            return new DStoreSurplusExcution<Void>(null, DStoreSurplusEnum.FAILURE);
        }
    }

    @ApiOperation(value = "确认入库", nickname = "dStoreSurplusConfirm", notes = "", tags={ "@岳希伟" })
    @ApiImplicitParam(name="uuid", value="报盈单id",paramType="query",dataType="string",required=true)
    @PostMapping(value = "editIn",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> editIn(HttpServletRequest request,@Validated @NotBlank(message="报亏单id不能为空")String uuid) {
        HttpSession session=request.getSession();
        DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String companyId=dUserInfo.getmId();
        String dealerId=dUserInfo.getdId();
        String operatorId=dUserInfo.getId();
        int editDStoreDeficitConfirm=dStoreSurplusService.editDStoreSurplusConfirm(companyId,dealerId,operatorId,uuid);
        if(editDStoreDeficitConfirm>0){
            return new DStoreDeficitExcution<Void>(null, DStoreDeficitEnum.SUCCESS);
        }else {
            return new DStoreDeficitExcution<Void>(null, DStoreDeficitEnum.FAILURE);
        }
    }


    @ApiOperation(value = "删除", nickname = "dStoreSurplusDelete", notes = "", tags={ "@岳希伟" })
    @ApiImplicitParam(name="uuid", value="报盈单id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="报亏单id不能为空")String uuid) {
        HttpSession session=request.getSession();
        DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String companyId=dUserInfo.getmId();
        String dealerId=dUserInfo.getdId();
        int deleteDStoreDeficit=dStoreSurplusService.deleteDStoreSurplus(companyId,dealerId,uuid);
        if(deleteDStoreDeficit>0){
            return new DStoreDeficitExcution<Void>(null, DStoreDeficitEnum.SUCCESS);
        }else {
            return new DStoreDeficitExcution<Void>(null, DStoreDeficitEnum.FAILURE);
        }
    }


    @ApiOperation(value = "编辑报盈单", nickname = "dStoreSurplusEdit", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "edit",
            produces = { MediaType.APPLICATION_JSON_VALUE },
            consumes = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> edit(HttpServletRequest request, @Validated @RequestBody DStoreSurplusEditDto editDto) {
        HttpSession session=request.getSession();
        DUserInfo dUserInfo= (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String companyId=dUserInfo.getmId();
        String dealerId=dUserInfo.getdId();
        try{
            int editSurplusBase=dStoreSurplusService.editSurplusBase(companyId,dealerId,editDto);
            if(editSurplusBase>0){
                return new DStoreDeficitExcution<Void>(null, DStoreDeficitEnum.SUCCESS);
            }else {
                return new DStoreDeficitExcution<Void>(null, DStoreDeficitEnum.FAILURE);
            }
        }catch (InsertInnerErrorException e){
            logger.error(e.getMessage(), e);
            return new DStoreDeficitExcution<Void>(null, DStoreDeficitEnum.FAILURE);
        }

    }

}
